package splug.cjk;

import android.app.*;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.*;

public class Menu extends Activity implements android.view.View.OnClickListener, OnKeyListener{
	
	private int[] menu = {R.id.menu1, R.id.menu2, R.id.menu3, R.id.menu4};
	private int[] views = {R.id.view1, R.id.view2, R.id.view3, R.id.view4};
	private int[] bbs = {R.id.menu1_1, R.id.menu1_2, R.id.menu1_3};
	private int[] friend = {R.id.menu3_1, R.id.menu3_2, R.id.menu3_3};
	private int[] report = {R.id.menu4_1, R.id.menu4_2};
	private ImageButton[] btn_menu;
	private ImageButton[] btn_bbs;
	private ImageButton[] btn_friend;
	private ImageButton[] btn_report;
	private String id;
	
	public static int counter = 0;
	
	
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		
		Bundle b = this.getIntent().getExtras();
		id = String.valueOf(b.getString("ID"));
		
		btn_menu = new ImageButton[4];
		btn_bbs = new ImageButton[4];
		btn_friend = new ImageButton[3];
		btn_report = new ImageButton[2];
		
		for(int i = 0; i < menu.length; i++){
			btn_menu[i] = (ImageButton) findViewById(menu[i]);
			btn_menu[i].setOnClickListener(this);
		}
		
		for(int i = 0; i < bbs.length; i++){
			btn_bbs[i] = (ImageButton) findViewById(bbs[i]);
			btn_bbs[i].setOnClickListener(this);
		}
		
		for(int i = 0; i < friend.length; i++){
			btn_friend[i] = (ImageButton) findViewById(friend[i]);
			btn_friend[i].setOnClickListener(this);
		}
		
		for(int i = 0; i < report.length; i++){
			btn_report[i] = (ImageButton) findViewById(report[i]);
			btn_report[i].setOnClickListener(this);
		}
		
		showContent(0);
		
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		Bundle b = null;
		
		for(int i = 0; i < btn_menu.length; i++){
			btn_menu[i].setSelected(false);
		}

		switch(v.getId()){
		case R.id.menu1:
			btn_menu[0].setSelected(true);
			showContent(0);
			break;
		case R.id.menu2:
			btn_menu[1].setSelected(true);
			b = new Bundle();
			b.putString("ID", id);
			intent = new Intent(Menu.this, FindCarPool.class);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu3:
			btn_menu[2].setSelected(true);
			showContent(2);
			break;
		case R.id.menu4:
			btn_menu[3].setSelected(true);
			showContent(3);
			break;
		case R.id.menu1_1:
			intent = new Intent(Menu.this, BBSNotice.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu1_2:
			intent = new Intent(Menu.this, BBSCarPool.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu1_3:
			intent = new Intent(Menu.this, BBSFree.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu3_1:
			intent = new Intent(Menu.this, Friend.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu3_2:
			intent = new Intent(Menu.this, Message.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu3_3:
			intent = new Intent(Menu.this, MyWriting.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu4_1:
			intent = new Intent(Menu.this, ReportTab1.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		case R.id.menu4_2:
			intent = new Intent(Menu.this, ReportTab2.class);
			b = new Bundle();
			b.putString("ID", id);
			intent.putExtras(b);
			startActivity(intent);
			break;
		}
		
	}
	
	private void showContent(int num){
		for(int i = 0; i < views.length; i++){
			View view = findViewById(views[i]);
			view.setVisibility((i == num) ? View.VISIBLE : View.GONE);
		}
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && counter == 0){
			Toast.makeText(Menu.this, "한번 더 누르시면 종료됩니다", Toast.LENGTH_SHORT).show();
			counter++;
			return true;
		}
		else if(keyCode == KeyEvent.KEYCODE_BACK && counter > 0){
			counter--;
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}